package com.example.sunjian.myapplication.event_bus;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sunjian.myapplication.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;


/**
 * 作者：sunjian on 16/6/28 11:33
 * 邮箱：sunjian@cyhz.com
 */
public class EventBusTestActivity extends Activity {


    private TextView mTv,mTv1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.event_bus);
        mTv =(TextView)findViewById(R.id.tv);
        mTv1=(TextView)findViewById(R.id.tv1);
        mTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(EventBusTestActivity.this,EventBusOtherActivity.class);
                startActivity(intent);
            }
        });
        EventBus.getDefault().register(this);

        mTv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        EventBus.getDefault().post("点击  mTv1 post");

                    }
                }.start();
            }
        });

    }


    @Subscribe
    public void onEvent(String msg){
        Log.e("sj","onEvent:"+msg);
//        mTv.setText(msg);

    }

    @Subscribe
    public void onEventMainThread(String msg){//在子线程中发布事件，在该方法中执行UI更新，依然报错。
        Log.e("sj","onEventMainThread:"+msg);
        mTv.setText(msg);

    }

    @Subscribe
    public void onEventBackgroundThread(String msg){
        Log.e("sj","onEventBackgroundThread:"+msg);
//        mTv.setText(msg);

    }

    @Subscribe
    public void onEventAsync(String msg){
        Log.e("sj","onEventAsync:"+msg);
//        mTv.setText(msg);

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
//        EventBus.getDefault().unregister(this);
    }
}
